<template>
  <section>
    <ym-dialog title="选择商品"
               top="50px"
               :visible.sync="showGoodsDialog"
               @confirm="confirmDialog">
      <el-form inline
               size="small"
               label-width="70px">
        <el-form-item label="型号">
          <el-input placeholder="请输入"
                    clearable
                    v-model.trim="params.barCode"> </el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input placeholder="请输入"
                    clearable
                    v-model.trim="params.goodsName"> </el-input>
        </el-form-item>
        <el-button type="primary"
                   size="small"
                   @click="handlerSearch">查找</el-button>
      </el-form>
      <ym-table :data='goodsList'
                :total="total"
                :loading='loading'
                :options="options"
                :currentPage="page"
                @load='getGoodsList'
                size="small"
                :set-router-page="false"
                @selection-change="handleSelectionChange"></ym-table>
    </ym-dialog>
  </section>
</template>
<script>
import { paramsFilter } from '@/utils/params-filter'
import { cloneDeep } from 'lodash'
import { accSub } from '@/utils/calculate'
export default {
  name: 'ym-goods-dialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    checkStorage: {
      type: Boolean,
      default: false
    },
    queryParams: {
      type: Object,
      default: () => ({})
    },
    hideStorage: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      showGoodsDialog: false,
      page: 1,
      size: 10,
      filterAnd: [],
      total: 0,
      params: {},
      goodsList: [],
      loading: false,
      options: {
        config: [
          {
            type: "selection",
            width: '55',
            align: "center",
            reserveSelection: true,
            selectable: (row, index) => {
              return !this.checkStorage || row.storage > 0
            },
          },
          {
            label: "序号",
            width: '55',
            align: "center",
            render: (h, params) => {
              return (
                <span>{`${Number(this.page - 1) * Number(this.size) + Number(params.$index + 1)}`}</span>
              )
            }
          },
          {
            prop: "barCode",
            label: '商品型号',
          },
          {
            prop: "goodsName",
            label: '商品名称',
          },
          {
            label: '单位',
            align: 'center',
            width: '80',
            render: (h, params) => {
              return (
                <span>{params.row.unitName}</span>
              )
            }
          },
          {
            label: '销售价',
            align: 'center',
            width: '100',
            render: (h, params) => {
              return (
                <span>{`￥${params.row.salePrice} / ${params.row.unitName}`}</span>
              )
            }
          },
          {
            prop: "pieceCount",
            label: '成件库存',
            align: "center"
          },
          {
            prop: "bulkCount",
            label: '散装库存',
            align: "center"
          },
        ]
      },
      selectGoodsList: [],
    }
  },
  watch: {
    visible (v) {
      this.showGoodsDialog = v
    },
    showGoodsDialog (v) {
      this.$emit('update:visible', v)
      if (v) {
        this.handlerSearch()
      }
    },
  },
  created () {

  },
  methods: {
    // 搜索
    handlerSearch () {
      this.filterAnd = paramsFilter(this.params, {
        like: ['goodsName', 'barCode'],
      })
      this.page = 1
      this.getGoodsList()
    },
    // 获取列表
    getGoodsList (page, size) {
      this.page = page || this.page
      this.size = size || this.size
      const data = [...this.filterAnd]
      for (var key in this.queryParams) {
        data.push({ [key]: this.queryParams[key] })
      }
      data.push({ status: 1 })
      this.loading = true
      this.$command.execute({
        api: "/admin-api/stock/query/list",
        parameter: {
          page: this.page,
          size: this.size,
          query: {
            filter: { and: data }
          }
        }
      }).then(res => {
        res.data.forEach(e => {
          e.storage = 0
          e.wareHouseOpts = []
          e.wareHouseCardOpts = []
          e.skuInfo.forEach(v => {
            v.lockStorage = Number(v.lockStorage) || 0
            e.storage += Number(accSub(v.storage, v.lockStorage))
            v.storage = Number(accSub(v.storage, v.lockStorage))
            if (e.wareHouseOpts.findIndex(i => i.id == v.wareHouseId) == -1) {
              e.wareHouseOpts.push({
                id: v.wareHouseId,
                wareHouseName: v.wareHouseInfo.wareHouseName
              })
            }
            if (e.wareHouseCardOpts.findIndex(i => i.id == v.wareHouseCardId) == -1) {
              e.wareHouseCardOpts.push({
                id: v.wareHouseCardId,
                wareHouseName: `${v.wareHouseCardInfo.wareHouseName}${!this.hideStorage ? "(库存【" + v.storage + "】)" : ''}`,
                storage: v.storage,
                parentId: v.wareHouseCardInfo.parentId,
                disabled: v.storage > 0 ? false : true
              })
            }
          })
          e.pieceCount = Math.floor(Number(e.storage) / Number(e.singleCount));
          e.bulkCount = Math.round(Number(e.storage) % Number(e.singleCount) * 100) / 100;
          e.unitName = e.unit ? e.unit.name : ''
          e.unitOpts = []
          e.unitOpts.push({
            key: e.unit.id,
            value: e.unit.name
          }, {
            key: 0,
            value: '件'
          })
        })
        this.goodsList = res.data
        this.total = res.total
        this.loading = false
      })
    },
    handleSelectionChange (list) {
      let data = cloneDeep(list)
      data.forEach(e => {
        e.pieceCount = 0
        e.bulkCount = 0
        e.count = 0
        e.refundFee = (Number(e.count) * Number(e.salePrice)).toFixed(2)
        e.goodsUnitName = e.unit ? e.unit.name : ''
        e.selectSku = null
        if (e.skuInfo.length == 1) {
          e.wareHouseId = e.skuInfo[0].wareHouseId
          e.wareHouseCardId = e.skuInfo[0].wareHouseCardId
          e.selectSku = e.skuInfo[0]
        }
      })
      this.selectGoodsList = data
    },
    confirmDialog () {
      this.$emit('confirm', this.selectGoodsList)
      this.selectGoodsList = []
      this.params = {}
      this.page = 1
    }
  }
}
</script>
